// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;

/**
 * The activator class controls the plug-in life cycle
 */
public class DenguesCorePlugin extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "org.dengues.core"; //$NON-NLS-1$

    // The shared instance
    private static DenguesCorePlugin plugin;

    /**
     * The constructor
     */
    public DenguesCorePlugin() {
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static DenguesCorePlugin getDefault() {
        return plugin;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getImageDescriptor".
     * 
     * @param path
     * @return
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getWarehousePluginService".
     * 
     * @return
     */
    public IWarehousePluginService getWarehousePluginService() {
        IDenguesService service = GlobalServiceFactory.getDefault().getService(IWarehousePluginService.class);
        return (IWarehousePluginService) service;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDesignerUIService".
     * 
     * @return
     */
    public IDesignerUIService getDesignerUIService() {
        IDenguesService service = GlobalServiceFactory.getDefault().getService(IDesignerUIService.class);
        return (IDesignerUIService) service;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDesignerUIService".
     * 
     * @return
     */
    public IDenguesUiService getDenguesUiService() {
        IDenguesService service = GlobalServiceFactory.getDefault().getService(IDenguesUiService.class);
        return (IDenguesUiService) service;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDesignerCoreService".
     * 
     * @return
     */
    public IDesignerCoreService getDesignerCoreService() {
        IDenguesService service = GlobalServiceFactory.getDefault().getService(IDesignerCoreService.class);
        return (IDesignerCoreService) service;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDenguesTitle".
     * 
     * @param s
     * @return
     */
    public String getDenguesTitle(String s) {
        IWorkbenchWindow activeWorkbenchWindow = getWorkbench().getActiveWorkbenchWindow();
        if (activeWorkbenchWindow != null) {
            IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
            if (activePage != null) {
                IPerspectiveDescriptor perspective = activePage.getPerspective();
                if (perspective != null) {
                    String label = perspective.getLabel();
                    Object headers = getDefault().getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
                    if (s.length() > 0) {
                        s = " - " + s; //$NON-NLS-1$
                    }
                    return label + s + "  - Dengues Studio (" + headers + ")"; //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
        }
        return ""; //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "log".
     * 
     * @param ex
     */
    public static void log(Throwable ex) {
        ExceptionOperation.operate(ex);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "logOpenMsg".
     * 
     * @param shell
     * @param mainMessage
     * @param detailMessage
     */
    public static void logOpenMsg(Shell shell, String mainMessage, String detailMessage) {
        ExceptionOperation.openErrorDilogWithDetail(shell, mainMessage, detailMessage);
    }
}
